/* ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   Module Name          : com.yaodian100.core.admin.web.UserQueryAction
   Module Description   :

   Date Created      : 2009/10/26
   Original Author   : jeff.ma
   Team              : yaodian100
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
   MODIFICATION HISTORY
   ------------------------------------------------------------------------------
   Date Modified       Modified by       Comments
   ------------------------------------------------------------------------------
   ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++*/
package com.yaodian100.core.admin.web;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;

import org.apache.commons.lang.xwork.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.yaodian100.core.admin.entity.Group;
import com.yaodian100.core.admin.entity.Role;
import com.yaodian100.core.admin.entity.User;
import com.yaodian100.core.admin.service.GroupService;
import com.yaodian100.core.admin.service.RoleService;
import com.yaodian100.core.admin.service.UserService;
import com.yaodian100.core.common.dao.impl.CommonCriteria;
import com.yaodian100.core.common.exception.CoreException;
import com.yaodian100.core.menu.entity.Menu;
import com.yaodian100.core.menu.service.MenuService;
import com.yaodian100.ec.common.web.AbstractAction;

/**
 * @author jeff.ma
 * 
 */
public class UserQueryAction extends AbstractAction {

	/** serialVersionUID */
	private static final long serialVersionUID = 4281715524222814657L;
	/** logger */
	private final Logger logger = LoggerFactory.getLogger(UserQueryAction.class);
	@Autowired
	private UserService userService;
	@Autowired
	private GroupService groupService;
	@Autowired
	private MenuService menuService;

	private Menu userStatus;
	private List<Group> groups;

	private List<User> users;
	private String usernameParam;
	private String emailParam;
	private String groupParam;
	private List<String> statusParam = new ArrayList();
	
	private Map<Role.Type,String> roleTypes = new TreeMap<Role.Type,String>();
	private List<Role> roles;
	private String roletypeParam;
	private String roleParam;
	@Autowired
	private RoleService roleService;
	/** default constructor */
	public UserQueryAction() {
	}

	/**
	 * @param userService the userService to set
	 */

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	/**
	 * @param groupService the groupService to set
	 */
	public void setGroupService(GroupService groupService) {
		this.groupService = groupService;
	}

	/**
	 * @return the usernameParam
	 */
	public String getUsernameParam() {
		return usernameParam;
	}

	/**
	 * @param usernameParam the usernameParam to set
	 */
	public void setUsernameParam(String usernameParam) {
		this.usernameParam = usernameParam;
	}

	/**
	 * @return the userStatus
	 */
	public Menu getUserStatus() {
		return userStatus;
	}

	/**
	 * @param userStatus the userStatus to set
	 */
	public void setUserStatus(Menu userStatus) {
		this.userStatus = userStatus;
	}

	/**
	 * @return the emailParam
	 */
	public String getEmailParam() {
		return emailParam;
	}

	/**
	 * @param emailParam the emailParam to set
	 */
	public void setEmailParam(String emailParam) {
		this.emailParam = emailParam;
	}

	/**
	 * @return the groupParam
	 */
	public String getGroupParam() {
		return groupParam;
	}

	/**
	 * @param groupParam the groupParam to set
	 */
	public void setGroupParam(String groupParam) {
		this.groupParam = groupParam;
	}

	/**
	 * @return the statusParam
	 */
	public List<String> getStatusParam() {
		return statusParam;
	}

	/**
	 * @param statusParam the statusParam to set
	 */
	public void setStatusParam(List<String> statusParam) {
		this.statusParam = statusParam;
	}

	/**
	 * @return the users
	 */
	public List<User> getUsers() {
		return users;
	}

	/**
	 * @param users the users to set
	 */
	public void setUsers(List<User> users) {
		this.users = users;
	}

	public String list() {
		return "list";
	}

	public String search() {
		CommonCriteria cri = new CommonCriteria();
		if (StringUtils.isNotEmpty(usernameParam)) {
			cri.addRlike("username", usernameParam);
		}
		if (StringUtils.isNotEmpty(groupParam)) {
			cri.addRlike("group.code", groupParam);
		}
		if (statusParam != null && statusParam.size() > 0) {
			cri.addIn("status", statusParam);
		}
		if (StringUtils.isNotEmpty(emailParam)) {
			cri.addRlike("email", emailParam);
		}
		
		Role.Type roleType = null;
		String roleKey = null;
		if (StringUtils.isNotEmpty(this.roleParam)) {
			roleKey = this.roleParam;
		} else {
			if (StringUtils.isNotEmpty(this.roletypeParam)) {
				roleType = Role.Type.valueOf(this.roletypeParam);
			}
		}
		
		try {
			// 仅维护 yaodain100 (A- 开头)与 crm user (C. 开头)
			cri.addIn("group.type", Arrays.asList("Y", "C"));
			//int entitySize = userService.getListSize(cri).intValue();
			int entitySize = userService.getListSizeByRole(cri, roleType, roleKey).intValue();
			getPager().setTotalSize(entitySize);
			//users = userService.getList(getPager().getCurrentPage() * getPager().getPageRecord(), getPager().getPageRecord(),
			//		cri, null);
			users = userService.getListByRole(getPager().getCurrentPage() * getPager().getPageRecord(), getPager().getPageRecord(), cri, null, roleType, roleKey);
			logger.info("pager:{},users:{}", getPager(), users);
		} catch (CoreException e) {
			addActionError(e.getMessage());
			e.printStackTrace();
		}
		return "list";
	}

	@Override
	public void prepare() throws Exception {
		super.prepare();
		clearErrorsAndMessages();

		if (userStatus == null) {
			userStatus = menuService.get("User.status");
			logger.debug("new user status:{}", userStatus);
		}

		if (groups == null) {
			CommonCriteria cri = new CommonCriteria();
			cri.addIn("type", Arrays.asList("Y", "C"));
			groups = groupService.getList(0, -1, cri, new String[] { "code asc" });
		}
		
		if (roleTypes.size() == 0) {
			roleTypes.put(Role.Type.OTHER, "一般角色");
			roleTypes.put(Role.Type.CATEGORY, "館線角色");
			roleTypes.put(Role.Type.LINEPM, "館線主管");
			roleTypes.put(Role.Type.MANAGER, "主管角色");
		}
	}
	
	public synchronized String getJsonRoles() {
		CommonCriteria cri = new CommonCriteria();
		if (StringUtils.isNotEmpty(this.roletypeParam)) {
			Role.Type t = Role.Type.valueOf(this.roletypeParam);
			cri.addEq("type", t);
		}
		
		//依UserMaintainAction中呈現的角色資料進行排序與相關處理
		String[] sort;
		if (this.roletypeParam.equals(Role.Type.MANAGER.getCode())) {
			sort = null;
		} else {
			sort = new String[] { "key" };
		}
		
		this.roles = this.roleService.getList(0, -1, cri, sort);
		
		if (this.roletypeParam.equals(Role.Type.MANAGER.getCode())) {
			for (int i = this.roles.size(); i > 0; i--) {
				Role r = roles.get(i - 1);
				if (!(r.getKey().startsWith("A-") || r.getKey().startsWith("C"))) {
					roles.remove(i - 1);
				}
			}
		}
		if (Role.Type.CATEGORY.getCode().equals(this.roletypeParam)) {
			Collections.sort(this.roles, new UserMaintainAction().getRoleComparator());
		}
		if (Role.Type.LINEPM.getCode().equals(this.roletypeParam)) {
			Collections.sort(this.roles, new UserMaintainAction().getRoleComparator());
		}
		return "jsonGetRoles";
	}

	public Map<Role.Type, String> getRoleTypes() {
		return roleTypes;
	}

	public void setRoleTypes(Map<Role.Type, String> roleTypes) {
		this.roleTypes = roleTypes;
	}

	public List<Group> getGroups() {
		return groups;
	}

	public void setGroups(List<Group> groups) {
		this.groups = groups;
	}

	public List<Role> getRoles() {
		return roles;
	}

	public void setRoles(List<Role> roles) {
		this.roles = roles;
	}

	public String getRoletypeParam() {
		return roletypeParam;
	}

	public void setRoletypeParam(String roletypeParam) {
		this.roletypeParam = roletypeParam;
	}

	public String getRoleParam() {
		return roleParam;
	}

	public void setRoleParam(String roleParam) {
		this.roleParam = roleParam;
	}


	
	
}
